package com.example.apps.view

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.example.apps.AppsIntent
import com.example.apps.AppsViewModel
import com.example.lib_base.BaseTitle
import com.example.lib_base.StateType
import com.example.lib_base.UiState
import com.huluobo.apps.entity.AppsEntity
import com.huluobo.apps.entity.AppsEntityItem
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Composable
fun AppsPage(navHostController: NavHostController,
             vm:AppsViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<AppsEntityItem>()
    }
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.OnError -> {
                        Log.e("TagA","获取应用程序列表失败:${it.msg}")
                    }

                    UiState.OnLoading -> {}
                    is UiState.OnSuccess<*> -> {
                        if (it.type == StateType.DEFAULT){
                            list.clear()
                            list.addAll(it.data as AppsEntity)
                        }
                    }
                }
            }
        }
        vm.sendIntent(AppsIntent.GetApps)
    })
    Column (Modifier.padding(10.dp)){
        BaseTitle("在线应用",navHostController)
        Spacer(Modifier.height(10.dp))
        LazyVerticalGrid(columns = GridCells.Fixed(3),
            content = {
                items(list){
                    AppsItem(it,vm, onclick = {},
                        longClick = {})
                }
            }, verticalArrangement = Arrangement.spacedBy(20.dp))
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun AppsItem(appsEntityItem: AppsEntityItem,
             vm:AppsViewModel,
             onclick:()->Unit,
             longClick:()->Unit) {
    Column(horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.combinedClickable(
            onClick = {},
            onLongClick = {
                longClick()
            }
        )) {
        AsyncImage(model = appsEntityItem.appIcon,
            contentDescription = "",
            modifier = Modifier.size(40.dp))
        Spacer(Modifier.height(10.dp))
        Text(text = appsEntityItem.appName)

    }

}